﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YodaMaster.Models;
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;


namespace YodaMaster.Controllers
{
    public class ScheduleCartController : Controller
    {


        ProgDatabase DB = new ProgDatabase();
        //
        // GET: /ShoppingCart/
        public ActionResult Index()
        {
            Database.DefaultConnectionFactory = new System.Data.Entity.Infrastructure.SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
            var cart = MyCart.GetCart(this.HttpContext);

            // Set up our ViewModel
            var viewModel = new CartViewModel
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };
            // Return the view
            return View(viewModel);
        }
        //
        // GET: /Store/AddToCart/5
        public ActionResult AddToCart(int id)
        {
            // Retrieve the album from the database
            Database.DefaultConnectionFactory = new System.Data.Entity.Infrastructure.SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
            var addedCourse = DB.Courses.Keys
                .Single(course => course.number == id);

            // Add it to the shopping cart
            var cart = MyCart.GetCart(this.HttpContext);

            cart.AddToCart(addedCourse);

            // Go back to the main store page for more shopping
            return RedirectToAction("Index");
        }
        //
        // AJAX: /ShoppingCart/RemoveFromCart/5
        
        public ActionResult RemoveFromCart(int id)
        {
            // Remove the item from the cart
            var cart = MyCart.GetCart(this.HttpContext);

            // Get the name of the album to display confirmation

               cart.RemoveFromCart(id);

              return  RedirectToAction("Index");
        }
        //
        // GET: /ShoppingCart/CartSummary
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = MyCart.GetCart(this.HttpContext);

            ViewData["CartCount"] = cart.GetCount();
            return PartialView("CartSummary");
        }

     
    }
}